package com.youkeda.meetingroom.service.impl;

import com.youkeda.meetingroom.dao.dataobject.MrMeetingRoom;
import com.youkeda.meetingroom.dao.mapper.MrMeetingRoomMapper;
import com.youkeda.meetingroom.model.MeetingRoom;
import com.youkeda.meetingroom.model.Result;
import com.youkeda.meetingroom.model.SearchParam;
import com.youkeda.meetingroom.service.SearchService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * SearchServiceImpl的注释
 *
 * @author: Awake
 * @date: 2019-07-03 17:36
 **/
@Service
public class SearchServiceImpl implements SearchService {


    @Resource
    MrMeetingRoomMapper mrMeetingRoomMapper;

    /**
     * 寻找最近的会议室列表
     * 根据给出参数进行筛选和排序
     * 只给出简略信息
     * 详细信息在会议室服务中获取
     *
     * @param searchParam 检索参数
     * @return 会议室列表（主键必须）
     */
    @Override
    public Result findMeetingRoom(SearchParam searchParam) {
        Result result = new Result();
        MrMeetingRoom mrMeetingRoom = new MrMeetingRoom();

        mrMeetingRoom.setCapacity(searchParam.getOutNumber());
        mrMeetingRoom.setBuilding(searchParam.getBuilding());
        mrMeetingRoom.setFloor(searchParam.getFloor());
        mrMeetingRoom.setHasComputer(searchParam.isHasComputer());
        mrMeetingRoom.setHasScreen(searchParam.isHasScreen());
        mrMeetingRoom.setHasProjector(searchParam.isHasProjector());
        mrMeetingRoom.setHasWhiteboard(searchParam.isHasWhiteboard());

        List<MrMeetingRoom> mrList = new ArrayList<>();
        if (searchParam.isSortByCapacity()==false) {
            mrList= mrMeetingRoomMapper.selectSortedByDistanceLimited(mrMeetingRoom,(searchParam.getPage()-1)*10,10);
        }
        else {
            mrList= mrMeetingRoomMapper.selectSortedByCapacityLimited(mrMeetingRoom,(searchParam.getPage()-1)*10,10);
        }
        if(mrList.size()==0){
            result.setErrorMessage("无满足条件的会议室，请重新选择");
            result.setSuccess(false);
            return result;
        }

        List<MeetingRoom> list= new ArrayList<>();
        for (int i = 0; i < mrList.size(); i++) {
            MeetingRoom meetingRoom = new MeetingRoom();
            BeanUtils.copyProperties(mrList.get(i), meetingRoom);
            if(!meetingRoom.isBeBooked() && !meetingRoom.isInMaintenance() && !meetingRoom.isInUse()){
                meetingRoom.setValid(true);
            }
            else {
                meetingRoom.setValid(false);
            }
            list.add(meetingRoom);
        }

        result.setData(list);
        result.setSuccess(true);

        return result;
    }
}
